package com.jiazhong.boot.redis.shuxin.test;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;

@SpringBootTest
@Slf4j
public class StringApp {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void a() {
        // 产生了一个对字符串操作的类
        ValueOperations<String, String> forValue = stringRedisTemplate.opsForValue();
        // 存放
        forValue.set("A", "aa");
        forValue.set("B", "23");
        forValue.set("C", "cc");
        forValue.set("D", "dd");
        forValue.set("C", "cc2");
        forValue.setIfAbsent("D", "dd2");
        forValue.setIfAbsent("E", "ee");
        forValue.setIfPresent("A", "aa2");
        forValue.setIfPresent("F", "ff");
        forValue.set("G", "gg", 1, TimeUnit.DAYS);
    }

    @Test
    public void b() {
        ValueOperations<String, String> forValue = stringRedisTemplate.opsForValue();
        String a = forValue.get("A");
        String c = forValue.getAndExpire("C", 1, TimeUnit.HOURS);
        String g = forValue.getAndPersist("G");
        String d = forValue.getAndDelete("D");
        String b = forValue.getAndSet("B", "88");
        log.info("a:{}", a);
        log.info("c:{}", c);
        log.info("g:{}", g);
        log.info("d:{}", d);
        log.info("b:{}", b);

    }

    @Test
    public void c() {
        ValueOperations<String, String> forValue = stringRedisTemplate.opsForValue();
        Long a = forValue.size("A");
        forValue.append("A","abcd");
        forValue.increment("B"); // +1
        forValue.increment("B",3); // +=3
        forValue.decrement("B"); // -1
        forValue.decrement("B",2); // -=2
        log.info("a:{}", a);
    }
}
